package me.zhengjie.repository;

import me.zhengjie.dto.sqlServer.ProductionInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.sql.Timestamp;
import java.util.List;

public interface ProductionInfoRepository extends JpaRepository<ProductionInfo, Integer>, JpaSpecificationExecutor<ProductionInfo> {

    ProductionInfo findProductionInfoByPalletCode(String palletCode);

    @Modifying
    @Query("update ProductionInfo pi set pi.layUpFinishTime=?1 , pi.productionStatus=2 where pi.id=?2")
    void updateProductionInfoLayUpFinishTime(Timestamp finishTime, Integer id);

    void deleteProductionInfoByPalletCodeAndProductionTypeAndProductionStatus(String palletCode, Integer productionType, Integer productionStatus);

    @Modifying
    @Query("update ProductionInfo pi set pi.productionType=2, pi.productionStatus=3, pi.pressingStartTime=?1 where pi.id=?2")
    void startPressing(Timestamp startTime, Integer id);

    @Modifying
    @Query("update ProductionInfo pi set pi.productionStatus=4, pi.pressingFinishTime=?1 where pi.palletCode=?2")
    void finishPressing(Timestamp finishTime, String palletCode);

    @Modifying
    @Query("update ProductionInfo pi set pi.productionType=3, pi.productionStatus=5, pi.unbindingBy=?1, pi.unbindingStartTime=?2, pi.unbindingStation=?5 where pi.palletCode=?3 and pi.id=?4")
    void startUnbindingProduction(String userName, Timestamp startTime, String palletCode, Integer id, String unbindingStation);

    @Modifying
    @Query("update ProductionInfo pi set pi.productionStatus=6, pi.unbindingFinishTime=?1 where pi.palletCode=?2 and pi.productionStatus=5 and pi.id=?3")
    void finishUnbindingProduction(Timestamp timestamp, String palletCode, Integer id);

    @Query("select pi from ProductionInfo pi where pi.productionStatus=?1 and pi.unbindingStation=?2 order by pi.unbindingStartTime")
    List<ProductionInfo> findProductionInfoByStatus(Integer status,String unbindingStation);

    @Query("select pi from ProductionInfo pi where pi.palletCode=?1 order by pi.layUpStartTime desc ")
    List<ProductionInfo> findProductionInfosOrderByLayUpStartTime(String palletCode);

    @Query("select pi from ProductionInfo pi where pi.palletCode=?1 order by pi.layUpFinishTime desc ")
    List<ProductionInfo> findProductionInfosOrderByLayUpFinishTime(String palletCde);

    @Query("select pi from ProductionInfo pi where pi.palletCode=?1 order by pi.id desc")
    List<ProductionInfo> findNewProductionInfo(String palletCode);
}
